using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Collections.ObjectModel;
using MediaSurface.MediaConsumerServiceReference;
using System.Collections;

namespace MediaSurface
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {

        private ObservableCollection<mediaConsumerListing> mediaLibraryList;

        static LibraryBar LibraryContent;
        

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SurfaceWindow1()
        {
            InitializeComponent();
            mediaLibraryList = new ObservableCollection<mediaConsumerListing>();
            
            AddActivationHandlers();

            
            InitializeSearchOption();

        }



        public void InitializeSearchOption()
        {
          
            SearchUserControl sControl = new SearchUserControl();
            
            libraryBarSuper.Content = sControl;

            LibraryContent = sControl.libraryBar2;
            
        
        }

             

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }


        
        private void scatterView1_Drop(object sender, SurfaceDragDropEventArgs e)
        {

            ScatterViewItem newItem = new ScatterViewItem { MinWidth = 430, MinHeight = 330 };
            
            // Rely on .ToString() on the data. A real app would do something more clever
            mediaConsumerListing listingItem = e.Cursor.Data as mediaConsumerListing;

            if (listingItem.aspect_ratio == 2)
            {
                newItem.MinWidth = 430;
                newItem.MinHeight = 330;
            }
            else
            {
                newItem.MinWidth = 430;
                newItem.MinHeight = 400;
            }
            // Place the new item at the drop location
            newItem.Center = e.Cursor.GetPosition(scatterView1);
            //newItem.Background = 
            int removedValue = LibraryContent.Items.IndexOf(listingItem);
           
            MediaConsumerClient client = new MediaConsumerClient();
            String title = listingItem.title;
            int id = listingItem.id;
            requestMediaStruct result = client.requestMedia(id, 3);
            string[] movieTags = listingItem.tags;
           
            SurfaceUserControl1 uControl =
                new SurfaceUserControl1(scatterView1, newItem, LibraryContent, removedValue, 
                listingItem.id, listingItem.title.ToString(), listingItem.length.ToString(), 
                listingItem.pool.ToString(), listingItem.aspect_ratio.ToString(),listingItem.thumbnailUrl, listingItem.description, movieTags);
          //  MediaDetailsUserControl mdControl = new MediaDetailsUserControl(scatterView1, LibraryContent, removedValue, listingItem.id, listingItem.title.ToString(), listingItem.length.ToString(),  listingItem.pool.ToString(), listingItem.aspect_ratio.ToString(),listingItem.thumbnailUrl, listingItem.description);
            uControl.SetMediaUrl(result.url); 
            newItem.Content = uControl;
            
            newItem.Center = new Point(460, 250);
            
            scatterView1.Items.Add(newItem);
         
            

        }


        
        
       
    }
}